import * as icons5icons from "@vicons/ionicons5"
import * as antdIcons from "@vicons/antd"
import { isEmpty, isString } from "@/utils"
import { NIcon } from "naive-ui"

const iconCollectMap: Recordable = {
    icons5: icons5icons,
    antd: antdIcons
}

/**
 * 返回 VNode 函数
 */
export const renderIcon = (icon: string | Component, attrs?: any, iconCollection?: string) => {
    let component
    if (isString(icon)) {
        if (iconCollection) {
            component = _findIcon(icon as string, iconCollectMap[iconCollection])
        } else {
            component = findIcon(icon as string)
        }
    } else {
        component = icon as Component
    }
    return () => h(NIcon, { component, ...attrs })
}

function _findIcon(icon: string, icons: any) {
    if (icons) {
        const iconMap: { [key: string]: any } = icons
        return iconMap[icon]
    }
    return undefined
}

export function findIcon(icon: string) {
    const components = Object.values(iconCollectMap)
        .map(v => _findIcon(icon, v))
        .filter(Boolean)
    return isEmpty(components) ? undefined : components[0]
}
